import Blockly from 'blockly/core'
import 'blockly/python_compressed'
import 'blockly/blockly_compressed'
import 'blockly/blocks_compressed'

Blockly.Blocks.math_operators = {
  init: function () {
    this.appendValueInput('leftValue').setCheck(null)
    this.appendDummyInput().appendField(
      new Blockly.FieldDropdown([
        ['+', 'Add()'],
        ['-', 'Sub()'],
        ['*', 'Mult()'],
        ['/', 'Div()'],
        ['**', 'Pow()'],
        ['//', 'FloorDiv()'],
        ['|', 'BitOr()'],
        ['&', 'BitAnd()'],
        ['%', 'Mod()'],
        ['^', 'BitXor()'],
        ['>>', 'RShift()'],
        ['<<', 'LShift()']
      ]),
      'Operators'
    )
    this.appendValueInput('rightValue').setCheck(null)
    this.setInputsInline(true)
    this.setOutput(true, null)
    this.setColour(230)
    this.setTooltip('')
    this.setHelpUrl('')
  }
}
const OPERATORS = {
  'Add()': '+',
  'Sub()': '-',
  'Mult()': '*',
  'Div()': '/',
  'Pow()': '**',
  'FloorDiv()': '//',
  'BitOr()': '|',
  'BitAnd()': '&',
  'Mod()': '%',
  'BitXor()': '^',
  'RShift()': '>>',
  'LShift()': '<<'
}

Blockly.Python.math_operators = function (block) {
  const leftValue = Blockly.Python.valueToCode(
    block,
    'leftValue',
    Blockly.Python.ORDER_NONE
  )
  const dropdownOperators = block.getFieldValue('Operators')
  const rightValue = Blockly.Python.valueToCode(
    block,
    'rightValue',
    Blockly.Python.ORDER_NONE
  )
  const code =
    leftValue + ' ' + OPERATORS[dropdownOperators] + ' ' + rightValue
  return [code, Blockly.Python.ORDER_NONE]
}

export default Blockly
